<template>
	<div class="">

		<div class="zzc" v-if="showzzc" @click="close"></div>

		<div class="container">
			<div class="title">扫码报修时间段管理</div>

			<div class="search_box">

				<div class="add_btn sear_btns" @click="openalert('add')" v-if="$permissions.indexOf(39)  != -1">
					<img src="@assets/images/add.png" alt="">
					新增
				</div>
				<div class="del_btn sear_btns" @click="delopen" v-if="$permissions.indexOf(40)  != -1">
					<img src="@assets/images/del.png" alt="">
					删除
				</div>
			</div>
			<div class="table-responsive mt-20">
				<h2 class="tcent" v-if="list.length ==0">暂时没有数据</h2>
				<table class="table table-hover" v-else>
					<tr>
						<th style="width: 50px;" v-if="$permissions.indexOf(40)  != -1">
							<input type="checkbox" @change="changeall" v-model="choseall" name="all" id="time"><label
								for="time"></label>
						</th>
						<th style="width: 20%;text-align: center;">星期</th>
						<th>上午时间</th>
						<th>下午时间</th>
					</tr>

					<tr v-for="item,index in list">
						<td style="width: 50px;" v-if="$permissions.indexOf(40)  != -1">
							<input type="checkbox" v-model="item.chosesf" @change="chosechange" name="chose"
								:id="'time'+item.id"><label :for="'time'+item.id"></label>
						</td>
						<td style="width: 20%;text-align: center;">{{item.week}}</td>
						<td>{{item.morningBeginTime}}-{{item.morningFinishTime}}</td>
						<td>{{item.afternoonBeginTime}}-{{item.afternoonFinishTime}}</td>

					</tr>
				</table>

				<!-- 	<div class="page_box">
					<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
						:current-page="page" :page-sizes="[10, 30, 60, 100]" :page-size="limit"
						layout="total, sizes, prev, pager, next, jumper" :total="inglen">
					</el-pagination>
				</div> -->
			</div>
		</div>

		<div class="alzzc" v-if="delalert">
			<div class="del_alert">
				<div class="modal-header tit" style="text-align: left;">
					<em></em>信息
					<div class="right" @click="delalert=false">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close"
							style="line-height: normal;"><span class="iconfont icon-cha" aria-hidden="true"></span>
						</button>
					</div>
				</div>
				<div class="alert_info">确认要删除选中的<span>{{delnum}}</span>条数据吗？</div>
				<div class="alert_btns" style="margin-top: 20px;">
					<div class="alert_sure" @click="del">确认</div>
					<div class="alert_close" @click="delalert=false">取消</div>
				</div>
			</div>
		</div>


		<!--新增时间段-->
		<div :class="alert_show?'modal in fade':'modal fade'" tabindex="-1" role="dialog" id="keshi"
			:style="alert_show?'display: block;z-index:199':''">
			<div class="cent w600" style="justify-content: center;width: 544px;height: 380px;">
				<div class="modal-dialog" role="document">
					<div class="modal-content">
						<div class="modal-header tit">
							<em></em>新增时间段
							<div class="right" @click="closeadd">
								<button type="button" class="close" data-dismiss="modal" aria-label="Close"
									style="line-height: normal;"><span class="iconfont icon-cha"
										aria-hidden="true"></span> </button>
							</div>
						</div>
						<!--表单-->
						<div class="modal-body h100 search-cont " id="weekalert">
							<div style="margin-left: 26px;">
								<el-checkbox name="week" @change="changeweek" v-model="week[0].check" value="星期一">星期一
								</el-checkbox>
								<el-checkbox name="week" @change="changeweek" v-model="week[1].check" value="星期二">星期二
								</el-checkbox>
								<el-checkbox name="week" @change="changeweek" v-model="week[2].check" value="星期三">星期三
								</el-checkbox>
								<el-checkbox name="week" @change="changeweek" v-model="week[3].check" value="星期四">星期四
								</el-checkbox>
								<el-checkbox name="week" @change="changeweek" v-model="week[4].check" value="星期五">星期五
								</el-checkbox>
								<el-checkbox name="week" @change="changeweek" v-model="week[5].check" value="星期六">星期六
								</el-checkbox>
								<el-checkbox name="week" @change="changeweek" v-model="week[6].check" value="星期日">星期日
								</el-checkbox>
							</div>


							<div class="line">
								<div class="line_left">上午：</div>
								<!-- 	<el-input placeholder="请输入内容" v-model="info.morningBeginTime" style="width: 120px;">
								</el-input> -->
								<el-time-picker style="width: 120px;" v-model="info.morningBeginTime" placeholder="请选择" :editable="false">
								</el-time-picker>
								<div class="line_heng">~</div>
								<el-time-picker style="width: 120px;" v-model="info.morningFinishTime"  :editable="false"
									placeholder="请选择">
								</el-time-picker>
							</div>
							<div class="line">
								<div class="line_left">下午：</div>
								<el-time-picker style="width: 120px;" v-model="info.afternoonBeginTime"  :editable="false"
									placeholder="请选择">
								</el-time-picker>
								<!-- <el-input placeholder="请输入内容" v-model="info.afternoonBeginTime" style="width: 120px;"> -->
								</el-input>
								<div class="line_heng">~</div>
								<el-time-picker style="width: 120px;" v-model="info.afternoonFinishTime"  :editable="false"
									placeholder="请选择">
								</el-time-picker>
							</div>
						</div>
						<div class="buttons">
							<div class="sure" @click="sure">保存</div>
							<div class="closebtn" @click="closeadd">取消</div>
						</div>
					</div>
				</div>
			</div>
		</div>

		<div class="loading-content" v-if="showloading">
			<div class="loading-spinner">
				<svg viewBox="25 25 50 50" class="circular">
					<circle cx="50" cy="50" r="20" fill="none" class="path"></circle>
				</svg>
			</div>
			<div class="loading-text">加载中...</div>
		</div>
	</div>
</template>

<script type="text/babel">

	export default {
		name: "",
		data() {
			return {
				showadd: false,
				showzzc: false,
				alert_show: false,
				delalert: false,
				choseall:false,
				page: 1,
				list: [],
				inglen: 0,
				delnum: 0,
				limit: 10,
				num: 1,
				showloading: false,
				ids:"",
				info:{
					morningBeginTime:"",
					morningFinishTime:"",
					afternoonBeginTime:"",
					afternoonFinishTime:"",
					weeks:[],
				},
				week:[
					{name:'星期一',check:false},
					{name:'星期二',check:false},
					{name:'星期三',check:false},
					{name:'星期四',check:false},
					{name:'星期五',check:false},
					{name:'星期六',check:false},
					{name:'星期日',check:false},
				],
				timevalue:"yyyy-MM-dd HH:mm:ss"
			}
		},
		components: {},

		created() {
			this.getlist();
		},

		mounted() {

		},

		methods: {
			// changetime(e){
			// 	if(new Date(e.picker.date).getHours() >= 12){
			// 		// e.picker.showSeconds=false
			// 		this.timevalue = "HH:mm"+':00'
			// 	}
			// },
			closeadd(){
				this.alert_show=false;
				this.options_show2=false;
				this.showzzc=false
				this.info.weeks = []
				for (var i = 0; i < this.week.length; i++) {
					this.week[i].check =false
				}
				console.log(this.info.weeks)
			},
			changeweek(e){
				console.log(e)
				console.log(this.week)
			},
			selectit2(index){
				this.ind = index
				this.options_show2=false
				this.showzzc=false
				this.$forceUpdate()
			},
			selectit(index){
				if(index=='all'){
					this.typename = '全部'
					this.typeid = ''
				}else{
					this.typename = this.types[index].name
					this.typeid = this.types[index].id
				}
				this.options_show=false
				this.showzzc=false
			},
			delopen(){
				var ids=''
				var num = 0
				for (var i = 0; i < this.list.length; i++) {
					if(this.list[i].chosesf==true){
						ids = ids+''+this.list[i].id+','
						num++
					}
				}
				this.ids = ids.substring(0,ids.length-1)
				if(this.ids==''){
					this.$Toast('暂无选中');
					return
				}
				this.delnum = num
				this.delalert=true	
			},
			// 删除
			del(){
				this.delalert=false		
				var param = {
					ids:this.ids
				}
				var that = this
				this.$http.post(this.$url + '/main/repairTime/delete', this.$qs.stringify(param))
					.then(function(response) {
						if (response.data.code == '1410') {
							that.$Toast(response.data.msg);
							sessionStorage.setItem('token', '')
							that.$router.push('/login')
							return
						}
						that.$Toast(response.data.msg);
					that.getlist()
					})
					.catch(function(error) {
						console.log(error);
						that.$Toast('网络拥堵，刷新一下');
					});
			},
			// 打开弹窗
			openalert(type,index){
				this.info = {
					morningBeginTime:"",
					morningFinishTime:"",
					afternoonBeginTime:"",
					afternoonFinishTime:"",
					weeks:[],
				}
				this.alert_show=true
			
			},
			changeall(){
				if(this.choseall){
					for (var i = 0; i < this.list.length; i++) {
						this.list[i].chosesf = true
					}
				}else{
					for (var i = 0; i < this.list.length; i++) {
						this.list[i].chosesf = false
					}
				}
			},
			chosechange(){
				for (var i = 0; i < this.list.length; i++) {
					if(this.list[i].chosesf == true){
						this.choseall=true
					}else{
						this.choseall=false
						break
					}
					
				}
			},
			//显示当前时间（年月日时分秒）
			timeFormate(timeStamp) {
			    
			      let hh =new Date(timeStamp).getHours() < 10? "0" + new Date(timeStamp).getHours(): new Date(timeStamp).getHours();
			      let mm =new Date(timeStamp).getMinutes() < 10? "0" + new Date(timeStamp).getMinutes(): new Date(timeStamp).getMinutes();
			      let ss =new Date(timeStamp).getSeconds() < 10? "0" + new Date(timeStamp).getSeconds(): new Date(timeStamp).getSeconds();
			  
			      return hh+":"+mm+':'+ss  ;
			    },
			// 表单提交
			sure() {
			
					if(!this.info.morningBeginTime && !this.info.morningFinishTime && !this.info.afternoonBeginTime && !this.info.afternoonFinishTime){
						this.$Toast('请至少填写一个时间段');
						return
					}
					if( this.info.morningBeginTime ){
						if(!this.info.morningFinishTime){
							this.$Toast('请填写上午结束时间');
							return
						}
					}
					if( this.info.morningFinishTime ){
						if(!this.info.morningBeginTime){
							this.$Toast('请填写上午开始时间');
							return
						}
					}
					if( this.info.afternoonBeginTime ){
						if(!this.info.afternoonFinishTime){
							this.$Toast('请填写下午结束时间');
							return
						}
					}
					if( this.info.afternoonFinishTime ){
						if(!this.info.afternoonBeginTime){
							this.$Toast('请填写下午开始时间');
							return
						}
					}
					
				// }else{
				// 	this.$Toast('请填写完整');
				// 	return
				// }
				// return
				if((new Date(this.info.afternoonBeginTime).getTime()>new Date(this.info.afternoonFinishTime).getTime())||(new Date(this.info.morningBeginTime).getTime()>new Date(this.info.morningFinishTime).getTime())){
					this.$Toast('请填写正确的时间段');
					return
				}
				// return
				
				this.info.afternoonBeginTime = !this.info.afternoonBeginTime?'':this.timeFormate(this.info.afternoonBeginTime)
				this.info.afternoonFinishTime =!this.info.afternoonFinishTime?'': this.timeFormate(this.info.afternoonFinishTime)
				this.info.morningBeginTime = !this.info.morningBeginTime?'':this.timeFormate(this.info.morningBeginTime)
				this.info.morningFinishTime = !this.info.morningFinishTime?'':this.timeFormate(this.info.morningFinishTime)
				
				for (var i = 0; i < this.week.length; i++) {
					if(this.week[i].check){
						this.info.weeks.push(this.week[i].name)
					}
				}
				
				if(this.info.weeks.length == 0){
					this.$Toast('请填写完整');
					return
				}
				this.info.weeks = this.info.weeks.join(',')
				
				if(!this.info.afternoonBeginTime){
					delete this.info.afternoonBeginTime
				}
				if(!this.info.afternoonFinishTime){
					delete this.info.afternoonFinishTime
				}
				if(!this.info.morningBeginTime){
					delete this.info.morningBeginTime
				}
				if(!this.info.morningFinishTime){
					delete this.info.morningFinishTime
				}
				
				console.log(this.info.weeks)
				
				this.showloading = true
				var that = this
				this.$http.post(this.$url + '/main/addRepairTime', this.$qs.stringify(this.info))
					.then(function(response) {
						if (response.data.code == '1410') {
							that.$Toast(response.data.msg);
							sessionStorage.setItem('token', '')
							that.$router.push('/login')
							return
						}
						that.$Toast(response.data.msg);
						that.alert_show = false
						that.info.weeks=[]
						for (var i = 0; i < that.week.length; i++) {
							that.week[i].check =false
						}
						that.getlist()
					})
					.catch(function(error) {
						console.log(error);
						that.$Toast('网络拥堵，刷新一下');
					});
			},
			handleSizeChange(val) {
			       console.log(`每页 ${val} 条`);
								this.limit = val
								this.choseall = false
								this.getlist()
			     },
			     handleCurrentChange(val) {
			       console.log(`当前页: ${val}`);
								this.page = val
				this.choseall = false
								this.getlist()
			     },
			
			getlist() {
				// this.showloading = true
				this.choseall = false

				var param = {
					page: this.page,
					limit: this.limit,
					// typeId:this.typeid==0?'':this.typeid,
					// brief:this.word
				}
				var that = this
				this.$http.get(this.$url + '/main/repairTime')
					.then(function(response) {
						if (response.data.code == '1410') {
							that.$Toast(response.data.msg);
							sessionStorage.setItem('token', '')
							that.$router.push('/login')
							return
						}
						that.list = response.data.data
						for (var i = 0; i < that.list.length; i++) {
							that.list[i].chosesf = false
						}
						that.choseall = false
						that.inglen = response.data.count
						if (that.inglen > that.limit) {
							that.num = Math.ceil(that.inglen / that.limit)
						} else {
							that.num = 1
						}
						that.showloading = false
					})
					.catch(function(error) {
						console.log(error);
						
						that.$Toast('网络拥堵，刷新一下');
					});

			},
		},

		computed: {},
	}
</script>
<style lang='less' scoped>
	/deep/ .el-input--prefix .el-input__inner {
		padding-left: 30px;
	}

	#weekalert .line {
		align-items: center;
	}

	.line_heng {
		margin: 0 10px;
	}

	/deep/ .el-pagination__total {
		height: 33px !important;
		line-height: 33px !important;
	}

	/deep/ .el-input--mini .el-input__inner {
		height: 33px;
	}

	/deep/.el-pagination__editor.el-input .el-input__inner {
		height: 33px;
	}

	.page_box {
		display: flex;
		justify-content: flex-end;
		margin-top: 30px;
	}

	/deep/ .el-pagination .btn-prev {
		border: 1px solid #ccc;
		padding: 0;
		border-right: none;
		min-width: 33px;
		height: 33px;
		padding-top: 2px;
	}

	/deep/ .el-pagination .btn-next {
		border: 1px solid #ccc;
		padding: 0;
		border-left: none;
		min-width: 33px;
		height: 33px;
		padding-top: 2px;
	}

	/deep/ .el-pager li {
		border: 1px solid #ccc;
		padding: 0;
		border-right: none;
		min-width: 33px;
		height: 33px;
		line-height: 33px;
	}

	/deep/ .el-pager li:nth-last-of-type(1) {
		border-right: 1px solid #ccc;
	}

	/deep/ .el-pager li.active {
		border: #4286f5 1px solid;
		background: #4286f5;
		color: #fff;
	}

	.pages {
		display: flex;
		justify-content: flex-end;
		margin-top: 10px;
	}

	.buttons {
		display: flex;
		align-items: center;
		justify-content: center;
		padding-bottom: 38px;
	}

	.closebtn {
		width: 89px;
		height: 34px;
		background: #EEEEEE;
		border-radius: 6px;
		font-size: 14px;
		color: #434343;
		text-align: center;
		line-height: 34px;
		cursor: pointer;
	}

	.sure {
		width: 89px;
		height: 34px;
		line-height: 34px;
		background: #4286F5;
		border-radius: 6px;
		font-size: 14px;
		color: #FFFFFF;
		margin-right: 18px;
		text-align: center;
		cursor: pointer;
	}

	.w600 .modal-body.h100 {
		height: 250px;
		overflow: visible;
	}

	.line textarea {
		height: 75px;
	}

	.line input,
	.line select,
	.line textarea {
		width: 360px;
		color: #000;
	}

	.line_title span {
		color: #E65100;
	}

	.line_title {
		font-size: 14px;
		color: #000000;
		line-height: 34px;
		width: 80px;
		text-align: right;
	}

	.line {
		display: flex;
		margin-top: 19px;
		justify-content: center;

	}

	.modal-header.tit {
		font-size: 20px;
		color: #000;
		font-weight: normal;
	}

	.w600 {
		margin: calc(50vh - 165px) auto;
	}

	.tcent {
		text-align: center;
	}

	input[type='checkbox'] {
		opacity: 0;
		position: absolute;
		z-index: -2;
	}

	input[type="checkbox"]+label {
		width: 16px;
		height: 16px;
		border: 1px solid #c8c8c8;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 0;
		box-sizing: border-box;
		margin-right: 7px;
	}

	input[type="checkbox"]+label::before {
		content: '';
		display: block;
		margin-bottom: 0;
		box-sizing: border-box;
	}

	input[type="checkbox"]:checked+label {
		border: 1px solid #0075ff;
		box-sizing: border-box;
		background: #0075ff url(../../../assets/images/dui.png) no-repeat;
		background-size: 80% 80%;
		background-position-x: 20%;
		background-position-y: 20%;
	}

	input[type="checkbox"]:checked+label::before {
		background: #0075ff;
	}

	.search_inp {
		width: 255px;
	}

	.title {
		font-size: 24px;
		color: #000000;
	}



	.btns {
		width: 72px;
		padding: 0;
	}

	.zzc {
		width: 100vw;
		height: 100vh;
		position: fixed;
		z-index: 4;
		top: 0;
		left: 0;
	}

	.loading-content {
		position: absolute;
		z-index: 2000;
		/* background-color: rgba(0, 0, 0, 0.8); */
		margin: 0;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		transition: opacity 0.3s;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		/* width: 200px;
		height: 150px;
		border-radius: 10px; */
	}

	.loading-spinner {
		text-align: center;
	}

	.loading-spinner .circular {
		height: 42px;
		animation: loading-rotate 2s linear infinite;
	}

	.loading-spinner .path {
		animation: loading-dash 1.5s ease-in-out infinite;
		stroke-dasharray: 90, 150;
		stroke-dashoffset: 0;
		/* stroke- 2; */
		stroke: #000;
		stroke-linecap: round;
	}

	.loading-text {
		font-size: 14px;
		color: #000;
	}

	@keyframes loading-rotate {
		100% {
			transform: rotate(360deg);
		}
	}

	@keyframes loading-dash {
		0% {
			stroke-dasharray: 1, 200;
			stroke-dashoffset: 0;
		}

		50% {
			stroke-dasharray: 90, 150;
			stroke-dashoffset: -40px;
		}

		to {
			stroke-dasharray: 90, 150;
			stroke-dashoffset: -120px;
		}
	}

	.biaoq {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
	}

	.qx:hover {
		background: #edf3fd;
		color: #4286f5;
	}


	.down {
		background: #fff;
		position: absolute;
		top: 30px;
		right: 0;
		width: 130px;
		text-align: left;
		padding: 0 5px;
		z-index: 5;

		color: #666;
		box-shadow: 0 0 10px rgb(0 0 0 / 20%);

	}

	.li {
		border-bottom: 1px solid #f3f3f3;
		padding-left: 20px;
		height: 40px;
		line-height: 40px;
	}

	.li:nth-last-of-type(1) {
		border-bottom: none;
	}

	.more {
		position: relative;
	}

	.btns .iconfont {
		font-size: 10px;
		transition: all .3s;
		transform: rotateZ(0deg);
		line-height: 26px;
	}

	.actives {
		background: #4286f5;
		color: #fff;
	}

	td,
	th {
		font-size: 14px;
		color: #333;
		/* line-height: 22px; */
		padding: 10px;
		border-top: 0;
		border-bottom: 1px solid #eee;
	}

	.modal {
		position: absolute;
	}

	.modal-dialog {
		height: 97%;
	}

	.modal-header {
		position: sticky;
		top: 0;
		z-index: 50;
	}

	.modal-content {
		/* width: 85%; */
		margin: 0 auto;
		max-height: 100%;
		overflow: visible;
		/* height: 65vh; */
		/* overflow-y: scroll; */
	}

	.cent {
		width: 100%;
		align-items: center;
	}
</style>
